# Contributor: Martell Malone <martell malone at gmail dot com>

_realname=glew
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.2.0
pkgrel=3
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
pkgdesc="GLEW, The OpenGL Extension Wrangler Library (mingw-w64)"
depends=()
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc")
license=('Modified BSD/MIT/GPL')
url="https://glew.sourceforge.io/"
source=("https://sourceforge.net/projects/glew/files/glew/${pkgver}/${_realname}-${pkgver}.tgz"
        001-makefile.patch)
sha256sums=('d4fc82893cfb00109578d0a1a2337fb8ca335b3ceccf97b97e5cc7f08e4353e1'
            'ab2e75b5161d60de68bd4b9b9ab1cc94d1b3db1ffc87e8a72f7a82f3ccfd440d')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/001-makefile.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      ../${_realname}-${pkgver}/build/cmake

  make
}

package () {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig
  sed \
    -e "s|@prefix@|${MINGW_PREFIX}|g" \
    -e "s|@libdir@|${MINGW_PREFIX}/lib|g" \
    -e "s|@exec_prefix@|${MINGW_PREFIX}/bin|g" \
    -e "s|@includedir@|${MINGW_PREFIX}/include|g" \
    -e "s|@version@|${pkgver}|g" \
    -e "s|@cflags@||g" \
    -e "s|@libname@|glew32|g" \
    -e "s|@libgl@||g" \
    -e "s|@requireslib@||g" \
    "${srcdir}"/${_realname}-${pkgver}/glew.pc.in \
    > "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/glew.pc
  # For building a static library or executable on Windows.
  # For more info see https://glew.sourceforge.net/install.html.
  echo "Cflags.private: -DGLEW_STATIC" \
    >> "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/glew.pc

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/doc/${_realname}
  cp -a "${srcdir}"/${_realname}-${pkgver}/doc "${pkgdir}"${MINGW_PREFIX}/share/doc/${_realname}/html

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
